package com.flx.cloud.shop.admin.controller;

import com.flx.cloud.shop.admin.entity.Employee;
import com.flx.cloud.shop.admin.service.EmployeeService;
import com.flx.common.utils.result.ResultResponse;
import com.flx.common.utils.validate.ValidateUtils;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author Fenglixiong
 * @Create 2022/5/3 1:35
 * @Description
 **/
@Api
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @GetMapping("/get/{id}")
    public ResultResponse get(@PathVariable("id")Long id){
        try {
            return ResultResponse.success(employeeService.get(id));
        }catch (Exception e){
            return ResultResponse.error("请求失败！");
        }
    }

    @PostMapping("/add")
    public ResultResponse add(@RequestBody Employee employee){
        return ValidateUtils.validate(employee,()-> {
            try {
                return ResultResponse.success(employeeService.add(employee));
            } catch (Exception e) {
                return ResultResponse.error("添加失败!");
            }
        });
    }

    @PostMapping("/update")
    public ResultResponse update(@RequestBody Employee employee){
        return ValidateUtils.validate(employee,()-> {
            try {
                return ResultResponse.success(employeeService.update(employee));
            } catch (Exception e) {
                return ResultResponse.error("修改失败!");
            }
        });
    }

    @GetMapping("/delete/{id}")
    public ResultResponse delete(@PathVariable("id")Long id){
        try {
            return ResultResponse.success(employeeService.delete(id));
        }catch (Exception e){
            return ResultResponse.error("删除失败！");
        }
    }

    @GetMapping("/getAuthor")
    public ResultResponse getAuthor(){
        try {
            return ResultResponse.success(employeeService.getAuthor());
        }catch (Exception e){
            return ResultResponse.error("请求失败！");
        }
    }

}
